import { IsNumber, IsOptional, IsString } from 'class-validator';
import { Transform, TransformFnParams } from 'class-transformer';

/**
 * 分页请求参数
 */
export class PaginationQueryDto {
  @IsOptional()
  @IsNumber({}, { message: '页码必须是数字' })
  @Transform(({ value }: TransformFnParams) => value ? Number(value) : 1)
  page: number = 1;

  @IsOptional()
  @IsNumber({}, { message: '每页数量必须是数字' })
  @Transform(({ value }: TransformFnParams) => value ? Number(value) : 10)
  pageSize: number = 10;

  @IsOptional()
  @IsString()
  @Transform(({ value }: TransformFnParams) => value || '')
  keyword?: string;
}

/**
 * 分页响应数据
 */
export class PaginationResponseDto<T> {
  data: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;

  constructor(data: T[], total: number, page: number, pageSize: number) {
    this.data = data;
    this.total = total;
    this.page = page;
    this.pageSize = pageSize;
    this.totalPages = Math.ceil(total / pageSize);
  }
}
